package com.br.android.widingwheels;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widingwheels.db.WidingWheelsDAO;
import com.br.android.widingwheels.entidades.Gasto;

@SuppressLint("NewApi")
public class EditarGastoActivity extends SherlockActivity {

	private EditText textDescricao;
	private EditText textValor;
	private Spinner spinnerCategoria;
	
	Currency currency = Currency.getInstance(Locale.getDefault());
	private static DecimalFormat format;
	
	private SimpleDateFormat formatData;
	
	private Gasto gasto;
	
	private WidingWheelsDAO dao;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_editar_gasto);
		
		format = new DecimalFormat(currency.getSymbol() + " ###,###,##0.00");
		formatData = new SimpleDateFormat("dd/MM/yyyy HH:ss");
		
		textDescricao = (EditText) findViewById(R.id.editTextAlterarNomeGasto);
		textValor = (EditText) findViewById(R.id.editTextAlterarValorGasto);
		spinnerCategoria = (Spinner) findViewById(R.id.spinnerAlterarCategoria);
		
		gasto = new Gasto();
		dao = new WidingWheelsDAO(this);
		
		Intent i = getIntent();
		gasto.setIdGasto(i.getExtras().getInt("idgasto"));
		gasto.setNomeGasto(i.getExtras().getString("descricao"));
		gasto.setValorGasto(i.getExtras().getDouble("valor"));
		
		textDescricao.setText(gasto.getNomeGasto());
		textValor.setText(String.valueOf(gasto.getValorGasto()));
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_adicionar_gasto, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuAdicionarGasto:
			if(textDescricao.getText().toString().isEmpty()){
				mensagem("Digite um nome para o gasto.");
			} else if (textValor.getText().toString().isEmpty()) {
				mensagem("Digite um valor.");
				
			} else {
				gasto.setNomeGasto(textDescricao.getText().toString());
				gasto.setValorGasto(Double.parseDouble(textValor.getText().toString()));
				gasto.setDataGasto(formatData.format(new Date()));
				gasto.setCategoriaGasto(spinnerCategoria.getSelectedItem().toString());
				dao.alterarGasto(gasto);
				startActivity(new Intent(this, PrincipalActivity.class));
				finish();
				overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			}
			break;
		case android.R.id.home:
			startActivity(new Intent(this, PrincipalActivity.class));
			finish();
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
		
	public void mensagem(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
}
